package user.view;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import user.dao.BookAdminDao;
import user.model.Book;
import user.util.StringUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class BookAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField bookName;
	private JLabel label2;
	private JTextField author;
	private JLabel label3;
	private JTextField type;
	private JLabel label5;
	private JTextField price;
	private JButton button2;
	private JButton button1;
	private BookAdminDao bookAdminDao = new BookAdminDao();
	private JLabel lblNewLabel;
	private JButton button;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			BookAddDialog dialog = new BookAddDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public BookAddDialog() {
		setTitle("图书添加");
		setBounds(100, 100, 490, 489);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(contentPanel, GroupLayout.PREFERRED_SIZE, 472, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(contentPanel, GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
		);
		JLabel label1 = new JLabel("图书名称");
		label1.setFont(new Font("宋体", Font.PLAIN, 18));
		bookName = new JTextField();
		bookName.setColumns(10);
		
		label2 = new JLabel("图书作者");
		label2.setFont(new Font("宋体", Font.PLAIN, 18));
		
		author = new JTextField();
		author.setColumns(10);
		
		label3 = new JLabel("图书类别");
		label3.setFont(new Font("宋体", Font.PLAIN, 18));
		
		type = new JTextField();
		type.setColumns(10);
		
		label5 = new JLabel("图书价格");
		label5.setFont(new Font("宋体", Font.PLAIN, 18));
		
		price = new JTextField();
		price.setColumns(10);
		
		button2 = new JButton("添加");
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookAddActionPerformed(e);
			}
		});
		button2.setFont(new Font("宋体", Font.PLAIN, 19));
		
		button1 = new JButton("重置");
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		button1.setFont(new Font("宋体", Font.PLAIN, 19));
		
		lblNewLabel = new JLabel("添加新图书");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 22));
		
		button = new JButton("返回");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(64)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
							.addGroup(gl_contentPanel.createSequentialGroup()
								.addComponent(label5)
								.addGap(18)
								.addComponent(price))
							.addGroup(gl_contentPanel.createSequentialGroup()
								.addComponent(label3)
								.addGap(18)
								.addComponent(type))
							.addGroup(gl_contentPanel.createSequentialGroup()
								.addComponent(label2)
								.addGap(18)
								.addComponent(author))
							.addGroup(gl_contentPanel.createSequentialGroup()
								.addComponent(label1)
								.addGap(18)
								.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
									.addComponent(lblNewLabel)
									.addComponent(bookName, GroupLayout.PREFERRED_SIZE, 177, GroupLayout.PREFERRED_SIZE))))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(button1, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
							.addGap(51)
							.addComponent(button2, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
							.addGap(52)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(50, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(35)
					.addComponent(lblNewLabel)
					.addGap(52)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label1)
						.addComponent(bookName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(39)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label2)
						.addComponent(author, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(38)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(type, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label3))
					.addGap(38)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(label5)
						.addComponent(price, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(button2, GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
						.addComponent(button1, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
						.addComponent(button, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		contentPanel.setLayout(gl_contentPanel);
		getContentPane().setLayout(groupLayout);
		this.setLocationRelativeTo(null);
	}

	protected void BookAddActionPerformed(ActionEvent e) {
		String bookName = this.bookName.getText();
		String author = this.author.getText();
		String type = this.type.getText();
		
		
		if(StringUtil.isEmpty(bookName)) {
			JOptionPane.showMessageDialog(null, "图书名称不能为空！");
			return;
		}
		if(StringUtil.isEmpty(author)) {
			JOptionPane.showMessageDialog(null, "作者不能为空！");
			return;
		}
		if(StringUtil.isEmpty(type)) {
			JOptionPane.showMessageDialog(null, "图书类别不能为空！");
			return;
		}
		Book book = null;
		try {
			Float price =Float.parseFloat(this.price.getText());
			book = new Book(bookName,author,type,price);
		} catch (Exception e2) {
			if(StringUtil.isEmpty(this.price.getText())) {
				JOptionPane.showMessageDialog(null, "图书价格不能为空！");
				return;
			}else {
				JOptionPane.showMessageDialog(null, "图书价格必须为数字！");
			}
			this.price.setText("");
			return;
		}
		
		try {
			int i = bookAdminDao.addBook(book);
			if(i>0) {
			JOptionPane.showMessageDialog(null, "添加图书《" + book.getBookName() + "》成功！");
			reset();
			}
		else {
			JOptionPane.showMessageDialog(null, "添加图书失败！");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	private void reset() {
		this.bookName.setText("");
		this.author.setText("");
		this.type.setText("");
		this.price.setText("");
	}

}
